<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增文件信息')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-info-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">文件名称：</label>
                <div class="col-sm-8">
                    <input id="fileName" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">文件类型：</label>
                <div class="col-sm-8">
                    <select name="fileType" class="form-control m-b"  th:with="type=${@dict.getType('sys_file_type')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                    <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 代码生成请选择字典属性</span>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">文件路径：</label>
                <div class="col-sm-8">
                    <input id="filePath" name="filePath" class="form-control" type="file">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script type="text/javascript">
        var prefix = ctx + "self/info"
        $("#form-info-add").validate({
            focusCleanup: true
        });


        function submitHandler() {
            if ($.validate.form()) {
                uploadFile();
            }
        }

        function uploadFile() {
            var formData = new FormData();
            var fileName1 = $("#fileName").val();


            var fileType =$("select[name='fileType']").val();

            if (fileName1 == null || fileName1 == "" ){
                $.modal.alertWarning("请填写文件名称");
                return false;
            }

            if ($('#filePath')[0].files[0] == null) {
                $.modal.alertWarning("请先选择文件路径");
                return false;
            }

            var fileName = $('#filePath')[0].files[0].name;
            var point = fileName.lastIndexOf(".");
            var type = fileName.substr(point);

            if(fileType == 1) {
                if (type!=".mp4" ) {
                    $.modal.alertWarning("请先选择mp4文件");
                    return false;
                }
            } else {
                if (type!=".jpg" && type!=".JPG" && type!=".png" && type!=".PNG") {
                    $.modal.alertWarning("请先选择jpg、png文件");
                    return false;
                }
            }

            formData.append('fileName', fileName1);
            formData.append('fileType', fileType);
            formData.append('file', $('#filePath')[0].files[0]);
            $.ajax({
                url: prefix + "/add",
                type: 'post',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                dataType: "json",
                success: function(result) {
                    $.operate.successCallback(result);
                }
            });
        }

    </script>
</body>
</html>